package com.example.myprojectoffeixue;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

public class WangJiMMActivity extends Activity {

    private EditText et;
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private HttpUtils httpUtils;
    private TextView tv;
    TimeCount time;
    boolean flag=false;
    MyApplication m;
    LinearLayout root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji_mm);
        info();
    }

    //获取控件
    private void info() {

        LinearLayout fanhui = (LinearLayout) findViewById(R.id.fh_wangji);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        time = new TimeCount(60000, 1000);
        et = (EditText) findViewById(R.id.et_regin);
        et1 = (EditText) findViewById(R.id.et1_regin);
        et2 = (EditText) findViewById(R.id.et2_regin);
        et3 = (EditText) findViewById(R.id.et3_regin);
        tv = (TextView) findViewById(R.id.tv_regin);
        root= (LinearLayout) findViewById(R.id.root);


        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){//获得焦点
                    flag=true;
                }
            }
        });


        et1.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){//获得焦点
                    flag=true;
                }
            }
        });


        et2.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){//获得焦点
                    flag=true;
                }
            }
        });


        et3.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){//获得焦点
                    flag=true;
                }
            }
        });














    }

    //根据Id的不同添加点击的监听事件
    public void click(View v) {

        switch (v.getId()) {
            //发送验证码，在这之前使用正则表达式对手机号进行验证
            case R.id.tv_regin:
                if ("".equals(et.getText().toString())) {
                    HttpConnection.showToast(getBaseContext(),"请输入手机号");

                } else {

                    UserClass u = new UserClass(et.getText().toString());
                    String j = JSON.toJSONString(u);
                    j = AESUtil.encrypt(j);
                    httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(j, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/sendCaptcha", params,
                            new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");
                                    tv.setClickable(true);

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if(arg0.result!= null && !"".equals(arg0.result)){
                                    arg0.result = AESUtil.decrypt(arg0.result);
                                    GateWay p = JSON.parseObject(arg0.result, GateWay.class);
                                    if (p.status == 0) {
                                    HttpConnection.showToast(getBaseContext(),"验证码已发送，请注意查收");
                                    time.start();}else{
                                        HttpConnection.showToast(getBaseContext(),p.msg);
                                    }

                                }else{
                                        HttpConnection.showToast(WangJiMMActivity.this,"服务器错误");
                                }}
                            });
                }
                break;

            case R.id.iv_regin:

                httpUtils = new HttpUtils();
                if ("".equals(et.getText().toString())) {
                    HttpConnection.showToast(getBaseContext(),"请输入手机号");
                } else if ("".equals(et1.getText().toString())) {
                    HttpConnection.showToast(getBaseContext(),"请输入验证码");
                } else if ("".equals(et2.getText().toString())) {

                    HttpConnection.showToast(getBaseContext(),"请输入密码");
                } else if ("".equals(et3.getText().toString()) || !et2.getText().toString().equals(et3.getText().toString())) {
                    HttpConnection.showToast(getBaseContext(),"请确认密码");
                } else {

                    UserClass f = new UserClass(et.getText().toString(), et1.getText().toString(), et2.getText().toString(), "Android");
                    String k = JSON.toJSONString(f);
                    k = AESUtil.encrypt(k);
                    RequestParams param = new RequestParams();
                    param.setHeader("Content-Type", "application/json");
                    try {
                        param.setBodyEntity(new StringEntity(k, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/forgetPassword", param,
                            new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");

                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !"".equals(arg0.result)) {
                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        GateWay p = JSON.parseObject(arg0.result, GateWay.class);
                                        if (p.status == 0) {
                                            finish();
                                        } else {
                                            HttpConnection.showToast(getBaseContext(),p.msg);
                                        }
                                    } else {
                                        HttpConnection.showToast(getBaseContext(),"修改失败，请重试");
                                    }
                                }
                            });

                }

                break;

            default:
                break;
        }

    }

    @Override
    protected void onStart() {


        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                root.getWindowVisibleDisplayFrame(r);
                int screenHeight =  root.getRootView().getHeight();
                int heightDifference = screenHeight - (r.bottom);
                if(flag){
                    if (heightDifference > 200) {
                        LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) root.getLayoutParams();
                        Log.d("sizeHight",screenHeight+"");
                        lp1.topMargin = -screenHeight/6;
                        root.requestLayout();
                    } else {
                        LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) root.getLayoutParams();
                        lp1.topMargin = 30;
                        root.requestLayout();
                    }
                }}
        });

        super.onStart();
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            tv.setText("获取验证码");
            tv.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            tv.setClickable(false);//防止重复点击
            tv.setText(millisUntilFinished / 1000 + "s");
        }


    }
}
